package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.BaseParamBo;
import com.cskaoyan.bean.BaseRespData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.admin.mall.bo.OrderRefundBo;
import com.cskaoyan.bean.admin.mall.bo.OrderShipBo;
import com.cskaoyan.bean.admin.mall.vo.OrderDetailVo;
import com.cskaoyan.service.admin.OrderService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name : OrderController.java
 * @Time : 2021/8/12 19:19
 * @Author : Xfeng
 * @Software : IntelliJ IDEA
 */

@RestController
@RequestMapping("admin/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequiresPermissions(value = {"*", "admin:order:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo list(String userId, String orderSn, Short[] orderStatusArray, BaseParamBo baseParamBo) {
        BaseRespData list = orderService.list(userId, orderSn, orderStatusArray, baseParamBo);
        return BaseRespVo.ok(list);
    }

    @RequiresPermissions(value = {"*", "admin:order:detail"}, logical = Logical.OR)
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        OrderDetailVo detail = orderService.detail(id);
        return BaseRespVo.ok(detail);
    }

    @RequiresPermissions(value = {"*", "admin:order:refund"}, logical = Logical.OR)
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody OrderRefundBo orderRefundBo) {
        orderService.refund(orderRefundBo);
        return BaseRespVo.ok();
    }

    @RequiresPermissions(value = {"*", "admin:order:ship"}, logical = Logical.OR)
    @RequestMapping("ship")
    public BaseRespVo ship(@RequestBody @Validated OrderShipBo orderShipBo, BindingResult bindingResult) {
        //TODO 正则校验生效了
        if (bindingResult.hasFieldErrors()) {
            return BaseRespVo.fail("快递编号有误");
        }
        if (orderShipBo.getShipSn() == null || orderShipBo.getShipSn().equals("")) {
            return BaseRespVo.fail("快递编号不能为空");
        }
        orderService.ship(orderShipBo);
        return BaseRespVo.ok();
    }
}
